<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PAD下单</title>
<jsp:include page="/header.jsp"></jsp:include>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
	$(function() {
		$(window).keydown(function(event){
			switch(event.keyCode) {
				case 13:scan();
			}
		});
	});
	
	function scan() {
		var code = $("#code").val();
		if(code==null||code=='') $.jqalert("请先扫描二维码！！");
		else {
			$.ajax({
				url:"${basePath}/pad/scan.htm",
				data:{code:code},
				dataType:"json",
				success:function(data) {
					if(!data.isSuccess) $.jqalert(data.msg, "提示");
					else {
						var product = data.json;
						var size = $("#qrcode_table").find("tr").size();
						var html = "<tr><td>"+size+"</td><td>"+product.name+"</td><td>"+
							product.code+"</td><td>"+product.productionyear+"</td><td>"+
							product.batch+"</td><td>"+(product.aunit=='片'?'散装':'整装')+"</td><td>"+
							findPriceByProductId(product.id)+"</td><td>"+
							"<a href='javascript:;' onclick='del(this);'>删除</a></td>"+
							"<input type='hidden' name='product_id' value='"+product.id+"'></tr>";
						$("#qrcode_table").append(html);
					}
				}
			});
		}
	}
	
	function findPriceByProductId(pid) {
		var trs = $("#product_table").find("tr:gt(0)");
		var price = 0;
		$.each(trs, function(i, n){
			var id = $(n).find("input[name='product_id']").val();
			if(id==pid) {
				price = $(n).find("td").eq(6).text();
				return false;
			}
		});
		return price;
	}
	
	function del(a) {
		var tr = $(a).parent().parent();
		var code = tr.find("td").eq(2).text();
		var codes = $.cookie("codes");
		codes = codes.replaceAll(code, "");
		$.cookie("codes", codes);
		tr.remove();
	}
	
	function confirm() {
		var trs = $("#qrcode_table").find("tr:gt(0)");
		var products = new Array();
		$.each(trs, function(i, n){
			var product = {};
			var id = $(n).find("input[name='product_id']").val();
			var code = $(n).find("td").eq(2).text();
			var name = $(n).find("td").eq(1).text();
			var price = $(n).find("td").eq(6).text();
			product.id = id;
			product.code = code;
			product.name = name;
			product.price = price;
			products.push(product);
		});
		$.cookie("pad_products", JSON.stringify(products));
		$.ajax({
			url:"${basePath}/pad/send.htm",
			data:{orderId:"${orderId}"},
			dataType:"json",
			success:function(data) {
				$.jqalert(data.msg, "提示", function() {
					if(data.isSuccess) {
						$.removeCookie("codes");
						$.removeCookie("pad_products");
						delAll();
						location = location;
					}
				});
			}
		});
	}
	
	function delAll() {
		$("#qrcode_table").find("tr:gt(0)").remove();
	}
</script>
</head>
<body>
    <div class="main">
    	<a href="${basePath }/pad/index.htm" class="yellow_btn btn">返回</a>
        <div class="cont">
            <h2 class="title">下单商品信息：</h2>
            <div class="ui">
                <table id="product_table" class="table_list">
                    <tr class="">
                      <th class="">序号</th>
                      <th>年份-商品名称-中间字</th>
                      <th class="">年份</th>
                      <th class="">批次</th>
                      <th class="">类型</th>
                      <th class="">数量</th>
                      <th class="">单价（元）</th>
                      <th class="">已发货数量</th>
                    </tr>
                    <c:forEach items="${items }" var="item" varStatus="status">
                    <tr class="">
                      <td class="">${status.index+1 }</td>
                      <td class="">${item.productionyear }-${item.goods_title }-${item.middleword }</td>
                      <td class="">${item.productionyear }</td>
                      <td class="">${item.batch }</td>
                      <td class="">${item.unit_display}</td>
                      <td class="">${item.goods_num }</td>
                      <td class="">${item.goods_price_display }</td>
                      <td class="">${item.out_num }</td>
                      <input type="hidden" name="product_id" value="${item.goods_id }">
                    </tr>
                    </c:forEach>
                </table>
            </div>
            <h2 class="title">未发货商品：</h2>
            <div class="ui">
                <div class="label_box">
                    <span class="sel_label fl">扫描商品二维码：</span>
                    <input type="text" id="code" class="text_input fl">
                    <a >
                </div>
            </div>
            <div class="ui">
                <table id="qrcode_table" class="table_list">
                    <tr class="">
                      <th class="">序号</th>
                      <th>品名</th>
                      <th class="">二维码</th>
                      <th class="">年份</th>
                      <th class="">批次</th>
                      <th class="">类型</th>
                      <th class="">单价（元）</th>
                      <th class="">操作</th>
                    </tr>
                </table>
            </div>
        <div class="text_c">
          <a href="javascript:;" class="yellow_btn btn" onclick="confirm();">确定发货</a>
        </div>
    </div>

</body>
</html>